/*
 *所有关于Mass_ProcessIns类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Mass_ProcessInsService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using iMES.Core.BaseProvider;
using iMES.Core.Extensions;
using iMES.Core.Extensions.AutofacManager;
using iMES.Core.Utilities;
using iMES.Entity.DomainModels;
using iMES.Mass.IRepositories;
using iMES.Mass.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Linq.Expressions;

namespace iMes.Mass.Services
{
    public partial class Mass_ProcessInsService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IMass_ProcessInsRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Mass_ProcessInsService(
            IMass_ProcessInsRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            AddOnExecuting = (Mass_ProcessIns model, object list) =>
            {
                if (string.IsNullOrWhiteSpace(model.Mass_ProcessIns_Code))
                    model.Mass_ProcessIns_Code = GetMass_DetectionItemsCode(model);
                if (repository.Exists(x => x.Mass_ProcessIns_Code == model.Mass_ProcessIns_Code))
                {
                    return webResponse.Error("过程检验单编号已存在");
                }
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
        /// <summary>
        /// 自动生成编码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string GetMass_DetectionItemsCode(Mass_ProcessIns model)
        {
            // 若有编码，则直接返回
            if (!string.IsNullOrEmpty(model.Mass_ProcessIns_Code))
            {
                return model.Mass_ProcessIns_Code;
            }
            else
            {
                var list = Mass_ProcessInsRepository.Instance.FindAsIQueryable
                    (x => x.Mass_ProcessIns_Code.StartsWith("GCJYD")).OrderByDescending(x => x.Mass_ProcessIns_Code).ToList().FirstOrDefault();
                if (list == null)
                {
                    return "GCJYD" + "00001";
                }
                else
                {
                    string code = list.Mass_ProcessIns_Code.Substring(5, 5);
                    int num = int.Parse(code) + 1;
                    return "GCJYD" + num.ToString().PadLeft(5, '0');
                }
            }
        }
    }
}
